/**
 * Author:yuiop
 * Date:2023/1/17 10:12
 */
package com.devops.admin.controller;

import cn.hutool.core.util.StrUtil;
import com.devops.admin.po.DOSBaseUserPo;
import com.devops.admin.service.DOSKubernetesResourceService;
import com.devops.admin.util.Assert;
import com.devops.common.utils.Message;
import com.devops.shiro.service.DOSBaseUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
@RequestMapping("/api/certmanager")
@Api(value = "/api/certmanager",tags = "certmanager相关接口")
public class CertManagerController {
    @Autowired
    DOSKubernetesResourceService dosKubernetesResourceService;
    @Autowired
    DOSBaseUserService dosBaseUserService;

    @PostMapping(value = "/info/{clusterName}/{namespace}")
    @ApiOperation(value="获取certManager信息",notes = "根据cluster和namespace，获取certManager信息")
    @ApiImplicitParams({@ApiImplicitParam(name = "clusterName",value = "clusterName",required = true,dataType = "String",paramType = "path"),
            @ApiImplicitParam(name = "namespace",value = "namespace",required = true,dataType = "String",paramType = "path")})
    public Message getCertManagerInfo(HttpServletRequest request, @PathVariable("clusterName") String clusterName, @PathVariable("namespace") String namespace) {

        checkApiToken(request);
        return Message.ok(dosKubernetesResourceService.getCertManagerInfo(clusterName, namespace));
    }

    @PostMapping(value = "/deal/{clusterName}/{namespace}/{certificateName}")
    @ApiOperation(value="获取certManager信息",notes = "根据cluster和namespace，获取certManager信息")
    @ApiImplicitParams({@ApiImplicitParam(name = "clusterName",value = "clusterName",required = true,dataType = "String",paramType = "path"),
            @ApiImplicitParam(name = "namespace",value = "namespace",required = true,dataType = "String",paramType = "path")})
    public Message dealCertManagerInfo(HttpServletRequest request, @PathVariable("clusterName") String clusterName,
                                       @PathVariable("namespace") String namespace,@PathVariable("certificateName") String certificateName) {
        checkApiToken(request);
        if (dosKubernetesResourceService.dealCertManagerInfo(clusterName, namespace, certificateName)) {
            return Message.ok("删除成功！");
        } else {
            return Message.error("删除失败！");
        }
    }

    public void checkApiToken(HttpServletRequest request) {
        String apiToken = request.getHeader("apiToken");
        Assert.isTrueMvc(StrUtil.isNotBlank(apiToken),"apiToken不能为空");
        List<DOSBaseUserPo> dosBaseUserPos = dosBaseUserService.getUserByApiToken(apiToken);
        Assert.isTrueMvc(dosBaseUserPos != null && dosBaseUserPos.size() == 1,"apiToken异常");
        DOSBaseUserPo dosBaseUserPo = dosBaseUserPos.get(0);
        Assert.isTrueMvc(dosBaseUserPo != null && StrUtil.isNotBlank(dosBaseUserPo.getId()) && dosBaseUserPo.getRoleId() == 1,"无权限");
    }
}
